package com.yangcangclub.commons.velocity;

import org.apache.velocity.Template;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模板资源引擎类(VelocityEngine)
 *
 *
 */
@Component
public class VMEngine {

    /** 默认模板资源提供者 */
    @Autowired
    private IVMTemplateProvider ivmTemplateProvider;

    /** 模板资源加载器 通过具体的模板资源提供者完成 */
    private Map<String, TemplateResourceLoader> resourceLoaders = null;

    /** 模板资源管理器加载器 */
    @Autowired
    private TemplateResourceManager templateResourceManager ;

    /** 扩展型运行期服务 */
    private ExtendRuntimeService extendRuntimeService;

    /** 默认字符集 */
    private String charset = Charset.forName("GBK").name();

    /**
     * VM引擎初始化
     * @throws Exception
     */
    @PostConstruct
    public void init() throws Exception {
        if (resourceLoaders == null) {
            resourceLoaders = new HashMap<String, TemplateResourceLoader>();
            TemplateResourceLoader defaultLoader = new TemplateResourceLoader();
            defaultLoader.setTemplateProvider(ivmTemplateProvider);
            defaultLoader.setCachingOn(ivmTemplateProvider.isCachingOn());
            resourceLoaders.put(ivmTemplateProvider.providerName(), defaultLoader);
        }
        this.templateResourceManager.setResourceLoaderMap(resourceLoaders);
        extendRuntimeService = new ExtendRuntimeService(this.templateResourceManager);
        templateResourceManager.initialize(extendRuntimeService);
        extendRuntimeService.init();
    }

    /**
     * 得到Template
     * @param providerName 模板提供者名称
     * @param tplName 具体模板名称
     * @return
     * @throws ResourceNotFoundException
     * @throws ParseErrorException
     * @throws Exception
     */
    public Template getTemplate(String providerName, String tplName)
            throws ResourceNotFoundException, ParseErrorException, Exception {
        String key = providerName + "$" + tplName;
        //运行期服务得到Template
        return extendRuntimeService.getTemplate(key,charset);
    }

    /**
     * 设置资源加载器的提供者
     * @param providerList
     */
    public void setProviderList(List<IVMTemplateProvider> providerList) {
        resourceLoaders = new HashMap<String, TemplateResourceLoader>();
        for (IVMTemplateProvider provider : providerList) {
            TemplateResourceLoader resourceLoadloader = new TemplateResourceLoader();
            //设置资源加载器的提供者
            resourceLoadloader.setTemplateProvider(provider);
            this.charset = provider.defaultCharset().name();
            resourceLoadloader.setCachingOn(provider.isCachingOn());
            resourceLoaders.put(provider.providerName(), resourceLoadloader);
        }
    }

    /**
     * 设置默认资源提供者
     * @param templateProvider
     */
    public void setTemplateProvider(IVMTemplateProvider templateProvider) {
        this.ivmTemplateProvider = templateProvider;
    }
}
